package StudyCode.summary.mycollection;

import java.util.Iterator;
import java.util.Map;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertsAStringToAnIteratorOfCharacters {
	public static Iterator<Character> toIterator(final String value) {
		return new Iterator<Character>() {
			int index = 0;

			@Override
			public boolean hasNext() {
				return index < value.length();
			}

			@Override
			public Character next() {
				return value.charAt(index++);
			}

			@Override
			public void remove() {
			}
		};
	}


	public static Iterator<?> toIterator(Map<?, ?> o) {
		final Iterator<?> it = o.entrySet().iterator();

		return new Iterator<Object>() {
			@Override
			public boolean hasNext() {
				return it.hasNext();
			}

			@Override
			public Object next() {
				Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
				return new Object[]{entry.getKey(), entry.getValue()};
			}

			@Override
			public void remove() {
			}
		};
	}
}
